import { Component, OnDestroy, ElementRef } from '@angular/core';
import { NavController ,ToastController } from '@ionic/angular';
import { ModalController } from '@ionic/angular';
import { ActivatedRoute, Params } from '@angular/router';
//引入插件
import { QRScanner, QRScannerStatus } from '@ionic-native/qr-scanner/ngx';
import { Router } from "@angular/router";
@Component({
  selector: 'app-scanner',
  templateUrl: './scanner.component.html',
  styleUrls: ['./scanner.component.scss'],
})
export class ScannerComponent implements OnDestroy  {
  public onlineTab="tab1"
  constructor(   
    public activeRoute: ActivatedRoute,
    public router:Router, 
    public toastController: ToastController,
     public nav: NavController,
     private scanner: QRScanner) {
    this.lightIcon = 'flash-off';
   }

   id=1;
  qrInfo="500";
  lightIcon: string;
  light: boolean;
  frontCamera: boolean;
  scannerClass: boolean;
  async presentToast(message) {
    const toast = await this.toastController.create({
      position: 'top',
      message:message ,
      duration: 2000
    });
    toast.present();
  }

  ionViewDidEnter() {
    this.scannerClass = true;
    this.startScanner();
    this.scanner.show();
  }

  ngOnDestroy() {
    this.scannerClass = false;
    this.destroyScanner();
  }
  


  toogleLight() {
    this.light = !this.light;

    if (this.light) {
      this.lightIcon = 'flash';
      this.scanner.enableLight();
    } else {
      this.lightIcon = 'flash-off';
      this.scanner.disableLight();
    }
  }

  toggleCamera() {
    this.frontCamera = !this.frontCamera;
    if (this.frontCamera) {
      this.scanner.useFrontCamera();
    } else {
      this.scanner.useBackCamera();
    }
  }

  
  closeModal() {
    let url="";
    this.activeRoute.queryParams.subscribe((source: Params) => {
      debugger
   if(source.id=="aa"){
       url="/mycenter/uploading";
    }else{
      url="/mycenter/uploadImg";
    }
    this.router.navigate([url], {
      queryParams: {
        object: this.qrInfo
      }
    })
    });
 
    this.destroyScanner();
  
  }
  destroyScanner() {
    this.scanner.destroy();
    // 这里延迟一秒将html背景色重新设置为白色，否则会变透明，影响视觉效果
    setTimeout(() => {
      (window.document.querySelector('html') as HTMLElement).style.backgroundColor = '#fff';
    }, 1000);
  }
  // 使用Barcode Scanner插件实现二维码、条形码扫描
  startScanner() {
    this.scanner.destroy();
    this.scanner.prepare().then((status: QRScannerStatus) => {
      if (status.authorized) {
        // camera permission was granted
        // start scanning
        let scanSub = this.scanner.scan().subscribe((text: string) => {
          if(text=="nanjingshichenggongzheng123698745666"){
            this.presentToast("'验证成功");
            this.qrInfo="200";
          }else{
            this.presentToast("验证失败");
            this.qrInfo="500";
          }
          console.log("-----------text-------------")
          console.log(text);
          this.scanner.hide(); // hide camera preview
          scanSub.unsubscribe(); // stop scanning
          this.closeModal()
        });

      } else if (status.denied) {
        this.scanner.openSettings();
      } else {
      }
    }).catch();
  }

}
